package com.ricky.net;

import java.io.IOException;
import java.net.InetSocketAddress;


import org.apache.log4j.Logger;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import com.ricky.core.net.abs.NetServer;


/**
 * 
 * 安全策略文件
 * @author rik
 *
 */
public class Policy extends NetServer implements IoHandler{

	protected static Logger log = Logger.getLogger(Policy.class);

	private IoAcceptor acceptor;
	
	public static String allow_all = "<cross-domain-policy>"
			+ "<allow-access-from  domain=\"*\"  to-ports=\"*\"/>"
			+ "</cross-domain-policy>\0";
	
	/**
	 * @param port
	 * @param maxConnection
	 */
	public Policy(int port, int maxConnection) {
		super(port, maxConnection);
	}

	public Policy(int port)
	{
		super(port, 5000);
	}
	
	/* (non-Javadoc)
	 * @see com.ricky.core.net.abs.NetServer#init()
	 */
	@Override
	public void init() {
		acceptor = new NioSocketAcceptor();
		acceptor.getSessionConfig().setMaxReadBufferSize(2048);
		acceptor.getFilterChain().addLast("Logging", new LoggingFilter());
		acceptor.getFilterChain().addLast("codec",
				new ProtocolCodecFilter(new TextLineCodecFactory()));
		acceptor.setHandler(this);

	}

	/* (non-Javadoc)
	 * @see com.ricky.core.net.abs.NetServer#start()
	 */
	@Override
	public void start() {
		try {
			acceptor.bind(new InetSocketAddress(port));
			log.info("安全策略文件启动"+port);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see org.apache.mina.core.service.IoHandler#exceptionCaught(org.apache.mina.core.session.IoSession, java.lang.Throwable)
	 */
	@Override
	public void exceptionCaught(IoSession arg0, Throwable arg1)
			throws Exception {
		log.error("minaServer.exceptionCaught", arg1);
	}

	/* (non-Javadoc)
	 * @see org.apache.mina.core.service.IoHandler#messageReceived(org.apache.mina.core.session.IoSession, java.lang.Object)
	 */
	@Override
	public void messageReceived(IoSession session, Object data) throws Exception {
		String c=data.toString();
		System.out.println(data);
		if(c.startsWith("GET") || c.startsWith("<policy-file-request/>"))
		{
		byte[] bytes = allow_all.getBytes();		
		IoBuffer buf = IoBuffer.allocate(bytes.length, false);
//		buf.order(ByteOrder.LITTLE_ENDIAN);
		buf.put(bytes);
		buf.flip();
		session.write(buf);
		session.close(false);
		}
	}

	/* (non-Javadoc)
	 * @see org.apache.mina.core.service.IoHandler#messageSent(org.apache.mina.core.session.IoSession, java.lang.Object)
	 */
	@Override
	public void messageSent(IoSession session, Object arg1) throws Exception {
//		log.info(arg0.getRemoteAddress()+"消息以发送:"+arg1);
//		session.close();
	}

	/* (non-Javadoc)
	 * @see org.apache.mina.core.service.IoHandler#sessionClosed(org.apache.mina.core.session.IoSession)
	 */
	@Override
	public void sessionClosed(IoSession session) throws Exception {
		
	}

	/* (non-Javadoc)
	 * @see org.apache.mina.core.service.IoHandler#sessionCreated(org.apache.mina.core.session.IoSession)
	 */
	@Override
	public void sessionCreated(IoSession session) throws Exception {
	}

	/* (non-Javadoc)
	 * @see org.apache.mina.core.service.IoHandler#sessionIdle(org.apache.mina.core.session.IoSession, org.apache.mina.core.session.IdleStatus)
	 */
	@Override
	public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {
		
	}

	/* (non-Javadoc)
	 * @see org.apache.mina.core.service.IoHandler#sessionOpened(org.apache.mina.core.session.IoSession)
	 */
	@Override
	public void sessionOpened(IoSession session) throws Exception {
		
	}
}
